package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.constant.Constant;
import com.demo.entity.Dictionary;
import com.demo.entity.Vo.DictionaryVo;
import com.demo.entity.from.DictionaryFrom;
import com.demo.mapper.DictionaryMapper;
import com.demo.response.Result;
import com.demo.service.IDictionaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据字典表非固定值的可后期增变的及全局信息如医院名称、医院logo等 服务实现类
 * @author zm
 * @since 2020-11-24
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {
    @Resource
    DictionaryMapper dictionaryMapper;

    /**
     *新增数据字典信息
     * @param dictionary       实体对象
     * @return
     */
    @Override
    public Result addDictionary(Dictionary dictionary) {
        //父级字典ID
        List<Integer> list = dictionaryMapper.getParentId();
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enumeration_name",dictionary.getEnumerationName());
        queryWrapper.in(list.size()>0,"parent_dictionary_code",list);
        if (dictionaryMapper.selectOne(queryWrapper) != null){
            return Result.error("数据字典名称已存在");
        }
        boolean flag = this.save(dictionary);
        return Result.success(flag);
    }

    /**
     * 更新数据字典信息
     * @param dictionary       实体对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateDictionary(List<DictionaryFrom> dictionary) throws Exception {
        for (DictionaryFrom from: dictionary){
            dictionaryMapper.updateDictionary(from);
        }
        return Result.success();
    }

    /**
     * 查询消息推送设置
     * @param parentDictionaryCode    上级字典编码【消息推送的默认为：0】
     * @return
     */
    @Override
    public Result getMesgPush(Integer parentDictionaryCode) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_dictionary_code",parentDictionaryCode);
        queryWrapper.isNull("deleted_user_id");
        List<Dictionary> list = dictionaryMapper.selectList(queryWrapper);
        List<DictionaryVo> dictionaryVoList = new ArrayList<>();

        for (Dictionary dictionary: list){
            DictionaryVo dictionaryVo = new DictionaryVo();
            QueryWrapper<Dictionary> query = new QueryWrapper<>();
            query.eq("parent_dictionary_code",dictionary.getDictionaryCode());
            query.isNull("deleted_user_id");
            BeanUtils.copyProperties(dictionary,dictionaryVo);
            dictionaryVo.setDictionaryList(dictionaryMapper.selectList(query));
            dictionaryVoList.add(dictionaryVo);
        }
        return Result.success(dictionaryVoList);
    }

    /**
     * 查询消息推送设置【定时任务】
     * @param parentDictionaryCode    上级字典编码
     * @return
     */
    @Override
    public List<DictionaryVo> getMesgPushTiming(Integer parentDictionaryCode) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_dictionary_code",parentDictionaryCode);
        queryWrapper.isNull("deleted_user_id");
        List<Dictionary> list = dictionaryMapper.selectList(queryWrapper);
        List<DictionaryVo> dictionaryVoList = new ArrayList<>();

        for (Dictionary dictionary: list){
            DictionaryVo dictionaryVo = new DictionaryVo();
            QueryWrapper<Dictionary> query = new QueryWrapper<>();
            query.eq("parent_dictionary_code",dictionary.getDictionaryCode());
            query.isNull("deleted_user_id");
            BeanUtils.copyProperties(dictionary,dictionaryVo);
            dictionaryVo.setDictionaryList(dictionaryMapper.selectList(query));
            dictionaryVoList.add(dictionaryVo);
        }
        return dictionaryVoList;
    }

    /**
     * 删除数据字典信息
     * @param dictionaryCode    字典编码
     * @param deletedUserId     删除人ID号
     * @return
     */
    @Override
    public Result deleteDictionary(Integer dictionaryCode, Integer deletedUserId) {
        Dictionary dictionary = new Dictionary();
        dictionary.setDictionaryCode(dictionaryCode);
        dictionary.setDeletedUserId(deletedUserId);
        dictionary.setDeletedTime(new Date());
        boolean flag = this.updateById(dictionary);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 数据字典详情
     * @param dictionaryCode   数据字典ID
     * @return
     */
    @Override
    public Dictionary getDictionaryDesc(Integer dictionaryCode) {
        Dictionary dictionary = this.getById(dictionaryCode);
        return dictionary;
    }

    /**
     * 消息推送查询
     * @param parentDictionaryCode    上级字典编码
     * @param enumeration_name         评估类型
     * @return
     */
    @Override
    public List<Dictionary> getMesgType(Integer parentDictionaryCode, String enumeration_name) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(enumeration_name != null,"enumeration_name",enumeration_name);
        queryWrapper.eq(parentDictionaryCode != null,"parent_dictionary_code",parentDictionaryCode);
        return dictionaryMapper.selectList(queryWrapper);
    }

    /**
     * 消息推送查询
     * @param parentDictionaryCode    上级字典编码
     * @param enumeration_name         评估类型
     * @return
     */
    @Override
    public Dictionary getMesgSingleObject(Integer parentDictionaryCode, String enumeration_name) {
        return dictionaryMapper.getMesgSingleObject(parentDictionaryCode, enumeration_name);
    }




    /**
     * 通过消息名称，获取消息ID
     * @param enumeration_name         评估类型名称
     * @return
     */
    @Override
    public Dictionary getMesgTypeById(String enumeration_name) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(enumeration_name != null,"enumeration_name",enumeration_name);
        return dictionaryMapper.selectOne(queryWrapper);
    }


    @Override
    public Dictionary getMesgStatus(Integer parentDictionaryCode,String enumeration_name,String enumeration_value) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(parentDictionaryCode != null,"parent_dictionary_code",parentDictionaryCode);
        queryWrapper.eq(enumeration_name != null,"enumeration_name",enumeration_name);
        queryWrapper.ne("enumeration_value","0");
        return dictionaryMapper.selectOne(queryWrapper);
    }

    @Override
    public Dictionary selectHospitalName() {
        return dictionaryMapper.selectById(28);
    }

    @Override
    public Dictionary selectHospitalLogo() {
        return dictionaryMapper.selectById(29);
    }


}
